package com.xy.blog.article.domain.dao.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xy.blog.article.api.model.SpecialColumnQuery;
import com.xy.blog.article.domain.dao.ISpecialColumnDao;
import com.xy.blog.article.domain.dao.mapper.ArticleSpecialColumnRelMapper;
import com.xy.blog.article.domain.dao.mapper.SpecialColumnMapper;
import com.xy.blog.article.domain.dao.model.ArticleSpecialColumnRelDO;
import com.xy.blog.article.domain.dao.model.SpecialColumnDO;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.id.SnowflakeIdWorker;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/7/17
 */
@Repository
public class SpecialColumnDaoImpl implements ISpecialColumnDao {


    @Autowired
    private SpecialColumnMapper specialColumnMapper;

    @Autowired
    private ArticleSpecialColumnRelMapper articleSpecialColumnRelMapper;

    @Override
    public PageResp<SpecialColumnDO> listByPage(PageReq<SpecialColumnQuery> req) throws BaseException {

        SpecialColumnQuery params = req.getParams();
        Page<Object> objects = PageHelper.startPage(req);
        Example example = new Example(SpecialColumnDO.class);
//        example.setOrderByClause("orders");
        Example.Criteria criteria = example.createCriteria();
        if (params !=null && StringUtils.isNotEmpty(params.getName())) {
            criteria.andEqualTo("name", params.getName());
        }
        List<SpecialColumnDO> specialColumnDOS = specialColumnMapper.selectByExample(example);
        return PageResp.data(specialColumnDOS,objects);
    }

    @Override
    public List<SpecialColumnDO> select(SpecialColumnDO item) {
        Example example = new Example(SpecialColumnDO.class);
//        example.setOrderByClause("orders");
        Example.Criteria criteria = example.createCriteria();
        String name = item.getName();
        if (StringUtils.isNotBlank(name)) {
            criteria.andLike("name", name);
        }
       return specialColumnMapper.selectByExample(example);
    }

    @Override
    public int selectCount(SpecialColumnDO item) {
        return specialColumnMapper.selectCount(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void save(SpecialColumnDO item) throws BaseException {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        LocalDateTime now = LocalDateTime.now();
        if (item.getCreateTime() == null) {
            item.setCreateTime(now);
        }
        if (item.getUpdateTime() == null) {
            item.setUpdateTime(now);
        }
        specialColumnMapper.insertSelective(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(SpecialColumnDO item) throws BaseException {
        specialColumnMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) throws BaseException {
        specialColumnMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SpecialColumnDO getById(Long id) throws BaseException {
        return specialColumnMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void saveRel(ArticleSpecialColumnRelDO item) {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        LocalDateTime now = LocalDateTime.now();
        if (item.getCreateTime() == null) {
            item.setCreateTime(now);
        }
        if (item.getUpdateTime() == null) {
            item.setUpdateTime(now);
        }
        articleSpecialColumnRelMapper.insertSelective(item);
    }

    @Override
    public List<ArticleSpecialColumnRelDO> selectRel(ArticleSpecialColumnRelDO item) {
        return articleSpecialColumnRelMapper.select(item);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteRelBySpecialColumnId(Long id) {
        articleSpecialColumnRelMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteRelByArticleId(Long id) {
        ArticleSpecialColumnRelDO articleSpecialColumnRelDO = new ArticleSpecialColumnRelDO();
        articleSpecialColumnRelDO.setArticleId(id);
        articleSpecialColumnRelMapper.delete(articleSpecialColumnRelDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteRelById(Long id) {
        articleSpecialColumnRelMapper.deleteByPrimaryKey(id);
    }
}
